//
//  OutLabel.m
//  for_test
//
//  Created by Carlson Lee on 2017/7/11.
//  Copyright © 2017年 Carlson Lee. All rights reserved.
//

#import "OutLabel.h"

@implementation OutLabel
{
    NSString * _text;
    UIFont * _font;
    UIColor * _backgroundColor;
    CGRect _frame;
}

- (void)setStr:(NSString *)str
{
    _str = str;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _frame = frame;
    }
    return self;
}

- (void)drawSubtractedText:(NSString *)text inRect:(CGRect)rect inContext:(CGContextRef)context
{
    //将当前图形状态推入堆栈
    CGContextSaveGState(context);
    
    //设置混合色
    CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
    
    //label上面添加label
//    UILabel *label = [[UILabel alloc] initWithFrame:rect];
//    label.font = _font;
//    label.text = text;
//    label.textAlignment = NSTextAlignmentCenter;
//    label.backgroundColor = _backgroundColor;
//    
//    [label.layer drawInContext:context];
    _font = [UIFont systemFontOfSize:18];
    CGSize ws = SDSize(text, _font, CGSizeZero);
    CGRect rt = CGRectMake(0, 0, ws.width, ws.height);
    
    [text drawInRect:rt withAttributes:@{NSFontAttributeName:_font, NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    //把堆栈顶部的状态弹出，返回到之前的图形状态
    CGContextRestoreGState(context);
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [self drawSubtractedText:_str inRect:_frame inContext:context];
}


@end
